<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Fit Layout</title>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type"/>
    <script src="../../../scripts/jquery.min.js"></script>
    <script src="../../../scripts/miniui/miniui.js"></script>
    <link href="../../../scripts/miniui/themes/default/miniui.css" rel="stylesheet">
    <link href="../../../scripts/miniui/themes/icons.css" rel="stylesheet">
    <link href="../../../scripts/miniui/themes/pure/skin.css" rel="stylesheet">
    <script src="../../../scripts/core.js"></script>

</head>
<body>
<style type="text/css">
    html, body {
        margin: 0;
        padding: 0;
        border: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
    }
</style>
<div class="mini-toolbar" style="padding:2px;border-bottom:0;">
    <table style="width:100%;">
        <tr>
            <td style="width:100%;">
                <a class="mini-button" iconCls="icon-add" onclick="addTask()" plain="true">新加任务</a>
                <a class="mini-button" iconCls="icon-upload" onclick="uploadIcon()" plain="true">上传任务图标</a>
                <a class="mini-button" iconCls="icon-save" onclick="saveTask()" plain="true">保存任务</a>
                <a class="mini-button" iconCls="icon-close" onclick="delTask()" plain="true">删除任务</a>
            </td>

        </tr>
    </table>
</div>

<div class="mini-fit">

    <mini-datagrid _dataField="taskType" allowCellEdit="true" allowCellSelect="true"
                   allowResize="true" allowSortColumn="false"
                   class="mini-datagrid"
                   id="taskDetail" idField="id" multiSelect="false"
                   onselectionchanged="onSelectionChanged"
                   pageSize="15"
                   showSortIcon="true"
                   style="width:100%;height:100%;"
                   url="/cmsapi/getTaskCfg">
        <columns>
            <column type="indexcolumn"></column>
            <column field="id">id

            </column>
            <column field="name" width="100">任务名称
                <input class="mini-textbox" property="editor" style="width:100%;"/>
            </column>

            <column field="energy" width="100">体力值奖励
                <input class="mini-spinner" maxValue="1000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="raceId" width="100">赛道id
                <input class="mini-spinner" maxValue="1000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="raceVal" width="100">代币数量
                <input class="mini-spinner" maxValue="1000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>
            <column field="orderIndex" width="100">排序字段
                <input class="mini-spinner" maxValue="1000000000" minValue="1" property="editor"
                       style="width:100%;"/>
            </column>

            <column field="des" width="100">描述
                <input class="mini-textbox" property="editor" style="width:100%;"/>
            </column>

            <column field="url" width="100">链接值
                <input class="mini-textbox" property="editor" style="width:100%;"/>
            </column>
            <column field="img" width="100">图片

            </column>
            <column field="verifyType" width="100">校验处理器
                <input class="mini-spinner" maxValue="1000000000" minValue="0" property="editor"
                       style="width:100%;"/>
            </column>


        </columns>
    </mini-datagrid>

</div>


<script type="text/javascript">

    var auth = GetParams();
    mini.parse();

    var taskDetailGrid = mini.get("taskDetail");
    taskDetailGrid.setAjaxOptions({
        headers: {"userId": auth.userId, "token": auth.token}
    });

    taskDetailGrid.on("drawcell", function (e) {
        var record = e.record,
            column = e.column,
            field = e.field,
            value = e.value;
        if (record.img == null) return;
        if (record.img == '') return;
        //将性别文本替换成图片
        if (column.field == "img") {
            e.cellHtml = '<img src="' + record.img + '" alt="Smiley face">';
        }

    });

    var descGrid = mini.get("descGrid");
    var titleGrid = mini.get("titleGrid");

    taskDetailGrid.load()


    function onSelectionChanged(e, record, c) {
        console.log("cccccccccccccccc")
        var grid = e.sender;
        var record = grid.getSelected();
        console.log(record)
        if (record) {
            descGrid.load({
                lid: record.descId
            });
            titleGrid.load({
                lid: record.titleId
            });
        } else {

        }


    }

    function uploadIcon() {
        if (taskDetailGrid.getSelecteds().length == 0) {
            mini.alert("没有选中任何数据")
            return;
        }
        var row = taskDetailGrid.getSelecteds()[0];
        mini.open({
            targetWindow: window,
            url: "UploadTaskIcon.html?userId=" + auth.userId + "&token=" + auth.token + "&taskId=" + row.id,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {
                taskDetailGrid.reload();
            }
        });
    }

    function addTitleLang() {

        if (taskDetailGrid.getSelecteds().length == 0) return;
        var row = taskDetailGrid.getSelecteds()[0];

        mini.open({
            targetWindow: window,
            url: "AddTaskLangCfg.html?userId=" + auth.userId + "&token=" + auth.token + "&lid=" + row.titleId,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {
                titleGrid.load({
                    lid: row.titleId
                });
            }
        });
    }

    function addDescLang() {

        if (taskDetailGrid.getSelecteds().length == 0) return;
        var row = taskDetailGrid.getSelecteds()[0];

        mini.open({
            targetWindow: window,
            url: "AddTaskLangCfg.html?userId=" + auth.userId + "&token=" + auth.token + "&lid=" + row.descId,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {
                descGrid.load({
                    lid: row.descId
                });
            }
        });
    }


    function addTask() {
        mini.open({
            targetWindow: window,
            url: "AddTaskCfg.html?userId=" + auth.userId + "&token=" + auth.token,
            title: "新增",
            width: 600,
            height: 400,
            onload: function () {

            },
            ondestroy: function (action) {
                taskDetailGrid.load();
            }
        });
    }


    function saveTask() {
        if (taskDetailGrid.getSelecteds().length == 0) {
            alert("没有选中任何行")
            return;
        }
        var row = taskDetailGrid.getSelecteds()[0];
        var json = mini.encode(row);
        $.ajax({
            url: "/cmsapi/editTaskCfg",
            data: json,
            type: "post",
            headers: {"userId": auth.userId, "token": auth.token},
            contentType: 'application/json',
            success: function (text) {
                taskDetailGrid.load({
                    typeId: row.typeId
                });
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    }


    function delDescData() {
        if (taskDetailGrid.getSelecteds().length == 0) {
            mini.alert("没有选中任何行")
            return;
        }
        if (descGrid.getSelecteds().length == 0) {
            mini.alert("没有选中任何行")
            return;
        }
        mini.confirm("确定删除记录？", "确定？",
            function (action) {
                if (action == "ok") {
                    var row = descGrid.getSelecteds()[0];
                    var task = taskDetailGrid.getSelecteds()[0];
                    var json = mini.encode(row);
                    descGrid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "/cmsapi/delLang",
                        data: json,
                        type: "post",
                        headers: {"userId": auth.userId, "token": auth.token},
                        contentType: 'application/json',
                        success: function (text) {
                            descGrid.load({
                                lid: task.descId
                            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                } else {

                }
            }
        );
    }

    function delTitleData() {
        if (taskDetailGrid.getSelecteds().length == 0) {
            mini.alert("没有选中任何行")
            return;
        }
        if (titleGrid.getSelecteds().length == 0) {
            mini.alert("没有选中任何行")
            return;
        }
        mini.confirm("确定删除记录？", "确定？",
            function (action) {
                if (action == "ok") {
                    var row = titleGrid.getSelecteds()[0];
                    var task = taskDetailGrid.getSelecteds()[0];
                    var json = mini.encode(row);
                    titleGrid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "/cmsapi/delLang",
                        data: json,
                        type: "post",
                        headers: {"userId": auth.userId, "token": auth.token},
                        contentType: 'application/json',
                        success: function (text) {
                            titleGrid.load({
                                lid: task.titleId
                            });
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                } else {

                }
            }
        );
    }


    function delTask() {
        if (taskDetailGrid.getSelecteds().length == 0) {
            alert("没有选中任何行")
            return;
        }
        mini.confirm("确定删除记录？", "确定？",
            function (action) {
                if (action == "ok") {
                    var row = taskDetailGrid.getSelecteds()[0];
                    var json = mini.encode(row);
                    taskDetailGrid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "/cmsapi/delTaskCfg",
                        data: json,
                        type: "post",
                        headers: {"userId": auth.userId, "token": auth.token},
                        contentType: 'application/json',
                        success: function (text) {
                            taskDetailGrid.load();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.responseText);
                        }
                    });
                } else {

                }
            }
        );
    }


</script>

</body>
</html>